package com.tmxk.api.days01.Ysc;

import javax.swing.text.html.HTMLDocument;
import java.util.*;

/**
 * HashMap
 * put()添加map集合元素
 * size() 集合获取大小
 * containsKey获取map集合对象是否存在
 * contains set集合对象名.contains()查找元素
 * remove()删除集合元素方式1 集合元素名.remove(对象元素)
 * remove()删除集合元素方式2 集合元素名.remove(下标位置)
 *
 */
public class HashMapDemo01 {
    public static void main(String[] args) {
        HashMap<String, Dog> hashMap = new HashMap<>();
        Dog dog1 = new Dog("小白", 3);
        Dog dog2 = new Dog("小白", 3);
        Dog dog3 = new Dog("小白", 3);
        Dog dog4 = new Dog("小白", 3);
        Dog dog5 = new Dog("小白", 3);

        hashMap.put(dog1.getName(), dog1);
        hashMap.put(dog2.getName(), dog2);
        hashMap.put(dog3.getName(), dog3);
        hashMap.put(dog4.getName(), dog4);
        hashMap.put(dog5.getName(), dog5);
        Collection<Dog> valesDog = hashMap.values();
        for (Dog dog : valesDog) {
            System.out.println(dog.getName() + "" + dog.getAge());
           //集合对象不能在for循环遍历中,执行remove方法,可通过迭代器进行remove移除,因迭代器专门为集合打造的的

            if ("小花".equals(dog.getName())){
                hashMap.remove(dog.getName());
            }
        }

        Set<String> keyDog = hashMap.keySet();
        for (String key : keyDog) {
            Dog dog = hashMap.get(key);
            System.out.println(dog.getName() + "--" + dog.getAge());
        }
        Iterator<Dog> itKey = valesDog.iterator();
        while (itKey.hasNext()) {
            Dog dog = itKey.next();
            System.out.println(dog.getName() + "--" + dog.getAge());
        }
        System.out.println("将hashMap集合作为一个整体输出");
        Set<Map.Entry<String,Dog>>setMap= hashMap.entrySet();
        setMap.forEach(map->{
            System.out.println(map);
        });
        boolean isFindKey=hashMap.containsKey("小白");

        //并发性和更新的
    }
}